# -*- coding:utf-8 -*-

from fastapi.exceptions import RequestValidationError
from starlette.exceptions import HTTPException as StarletteHTTPException
from fastapi import Request
from fastapi.responses import JSONResponse
from fastapi.encoders import jsonable_encoder

async def exception_handlers(request:Request, exc: Exception):
    return JSONResponse({
        "code": 500,
        "message": str(exc),
        "success": False,
        "data": None,
    }, 200)

async def http_exception_handler(request: Request, exc: StarletteHTTPException):
    return JSONResponse({
        "code": exc.status_code,
        "message": str(exc.detail),
        "success": False,
        "data": None,
    }, 200)
    
async def validation_exception_handler(request: Request, exc: RequestValidationError):
    return JSONResponse({
        "code": 422,
        "message": jsonable_encoder({"detail": exc.errors(), "body": exc.body}),
        "success": False,
        "data": None,
    }, 200)
